package mplayer;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class Playlist {

	private Vector<File> playlist_;
	private JScrollPane splist_;
	private JPanel jpbuts_;
	private JList jlist_;
	private JFrame f_;

	
//	InputStreamReader in = new InputStreamReader(httpCon.getInputStream());
//	BufferedReader br = new BufferedReader(in)
	// Buttons
	private JButton jbremove_; 
	private JButton jbclear_;
	private JButton jbdown_; 
	private JButton jbadd_;
	private JButton jbup_;


	public Playlist() {
		initiatePlaylist();
	}

	/**
	 * Makes the playlist frame visible.
	 */
	public void showPlaylist() {
		f_.setVisible(true);
	}

	/**
	 * Makes the playlist frame invisible.
	 */
	public void hidePlaylist() {
		f_.setVisible(false);
	}

	/**
	 * Initiates the window by creating the frame, buttons, lists and adding listeners to handle events.
	 */
	private void initiatePlaylist() {
		playlist_	= new Vector<File>();
		f_ 			= new JFrame("Playlist");
		jlist_ 		= new JList(playlist_);
		splist_ 	= new JScrollPane();
		jpbuts_ 	= new JPanel(new FlowLayout(FlowLayout.LEFT));
		jbadd_ 		= new JButton("+");
		jbremove_ 	= new JButton("-");
		jbup_ 		= new JButton("Up");
		jbdown_ 	= new JButton("Down");
		jbclear_	= new JButton("Clear");

		/* listeners */
		jbadd_.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// ADD
			}
		});

		jbremove_.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// REMOVE
			}
		});
		jbup_.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// +
			}
		});
		jbdown_.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// -
			}
		});

		jbclear_.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// clear
			}
		});
		splist_.getViewport().add(jlist_);

		jlist_.addMouseListener(new MouseListener() {
			public void mouseReleased(MouseEvent e) {}
			public void mousePressed(MouseEvent e) {}
			public void mouseExited(MouseEvent e) {}
			public void mouseEntered(MouseEvent e) {}
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) {
					int i = jlist_.getSelectedIndex();
					// Play selected.
				}
			}
		});

		// Button sizes.
		jbadd_		.setPreferredSize(new Dimension(30, 20));
		jbremove_	.setPreferredSize(new Dimension(30, 20));
		jbup_		.setPreferredSize(new Dimension(50, 20));
		jbdown_		.setPreferredSize(new Dimension(50, 20));
		jbclear_	.setPreferredSize(new Dimension(50, 20));
		splist_		.setPreferredSize(new Dimension(200, 250));

		// Adding buttons to panel.
		jpbuts_.add(jbadd_);
		jpbuts_.add(jbremove_);
		jpbuts_.add(jbup_);
		jpbuts_.add(jbdown_);
		jpbuts_.add(jbclear_);

		// Adding list and buttons to the frame.
		f_.getContentPane().add(splist_, BorderLayout.CENTER);
		f_.getContentPane().add(jpbuts_, BorderLayout.PAGE_END);

		f_.pack();
		f_.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f_.setVisible(false);
	}

	public void addFileToPlaylist(File f) {
		playlist_.add(f);
		//updatePlaylist();
	}

	private int playingNumber_ = 2;
	public void updatePlaylist() {
		jlist_.setListData(playlist_);
System.out.println(jlist_.getComponentCount());
		Component c = jlist_.getComponent(0);
		c.setBackground(Color.cyan);

		jlist_.repaint();
	}

	public static void main(String [] args) {
		Playlist pl = new Playlist();
		//
		pl.addFileToPlaylist(new File("Thrice - Daedalus"));
		pl.addFileToPlaylist(new File("Thrice - For Miles"));
		pl.addFileToPlaylist(new File("Brand New - Guernica"));
		pl.addFileToPlaylist(new File("Moving Mountains - The Cascade"));
		pl.addFileToPlaylist(new File("Moving Mountains - Parts in Different Places"));
		pl.addFileToPlaylist(new File("Dustin Kensrue - Blanket of Ghosts"));
		
		pl.updatePlaylist();

		pl.showPlaylist();

	}

}
